clear all;
clc;

% ------------------------ Import Data ------------------------------------

pol = -1;

file_epimp = 'neg_epi_mp_dc_act_100axons_312nodes_9um.txt';
file_epibp = 'neg_epi_bp_dc_act_100axons_312nodes_9um.txt';
file_submp = 'neg_sub_mp_dc_act_100axons_312nodes_9um.txt';
file_subbp = 'neg_sub_bp_dc_act_100axons_312nodes_9um.txt';

th_dev = [0,-10,-20];
nodev  = length(th_dev);
noaxons = 100;

epimp_data = zeros(noaxons,nodev);
epibp_data = zeros(noaxons,nodev);
submp_data = zeros(noaxons,nodev);
subbp_data = zeros(noaxons,nodev);

for k = 1:nodev
    
    dirname = ['deviation_',strrep(num2str(th_dev(k)),'-','n')];
    cd(dirname);
    
    % epidural monopolar
    epimp_tmp = load(file_epimp);
    epimp_data(:,k) = sort(epimp_tmp(:,1)*pol);
    % epidural bipolar
    epibp_tmp = load(file_epibp);
    epibp_data(:,k) = sort(epibp_tmp(:,1)*pol);
    % subdural monopolar
    submp_tmp = load(file_submp);
    submp_data(:,k) = sort(submp_tmp(:,1)*pol);
    % subdural bipolar
    subbp_tmp = load(file_subbp);
    subbp_data(:,k) = sort(subbp_tmp(:,1)*pol);
    
    cd ..;
    
end

% ------------------------ Processing Data --------------------------------

epimp_min = min(epimp_data,[],2);
epimp_max = max(epimp_data,[],2);

epibp_min = min(epibp_data,[],2);
epibp_max = max(epibp_data,[],2);

submp_min = min(submp_data,[],2);
submp_max = max(submp_data,[],2);

subbp_min = min(subbp_data,[],2);
subbp_max = max(subbp_data,[],2);

% ------------------------ Plotting Data ----------------------------------

fact = 100*(1/noaxons:1/noaxons:1);
fact = fact';

figure;
subplot(1,2,1);
hold on;
fill(cat(1,epimp_min,flipud(epimp_max)),cat(1,fact,flipud(fact)),[0.5,0.5,0.5],'LineWidth',2);
fill(cat(1,submp_min,flipud(submp_max)),cat(1,fact,flipud(fact)),'k','LineWidth',2);
hold off;
title('Monopolar Stimulation','FontSize',30);
xlabel('Cathodic Stimulation Voltage (V)','FontSize',30);
ylabel('DC Fibers Activated (%)','FontSize',30);
set(gca,'FontSize',26);

subplot(1,2,2);
hold on;
fill(cat(1,epibp_min,flipud(epibp_max)),cat(1,fact,flipud(fact)),[0.5,0.5,0.5],'LineWidth',2);
fill(cat(1,subbp_min,flipud(subbp_max)),cat(1,fact,flipud(fact)),'k','LineWidth',2);
hold off;
title('Bipolar Stimulation','FontSize',30);
xlabel('Cathodic Stimulation Voltage (V)','FontSize',30);
ylabel('DC Fibers Activated (%)','FontSize',30);
legend('Epidural','Intradural','Location','E');
set(gca,'FontSize',26);

figure;
hold on;
fill(cat(1,submp_min,flipud(submp_max)),cat(1,fact,flipud(fact)),'k','LineWidth',2);
fill(cat(1,subbp_min,flipud(subbp_max)),cat(1,fact,flipud(fact)),[0.5,0.5,0.5],'LineWidth',2);
hold off;
title('Intradural Stimulation','FontSize',36,'FontWeight','b');
xlabel('Cathodic Stimulation Voltage (V)','FontSize',30);
ylabel('DC Fibers Activated (%)','FontSize',30);
legend('Monopolar','Bipolar','Location','E');
set(gca,'FontSize',26);